package cn.lijiguo.concurrency.part1.threadobjectmethods;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * SleepDontReleaseLock
 * 演示 sleep 不释放 lock (lock 本身需要手动释放)
 * @author lijiguo
 * @date 2020/1/7 13:49
 */
public class SleepDontReleaseLock implements Runnable {

    private static final Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        SleepDontReleaseLock r = new SleepDontReleaseLock();
        new Thread(r, "t1").start();
        new Thread(r, "t2").start();
    }

    @Override
    public void run() {
        // 上锁
        lock.lock();
        System.out.println("线程 " + Thread.currentThread().getName() + " 获取到了锁");

        try {
            Thread.sleep(5000);
            System.out.println("线程 " + Thread.currentThread().getName() + " sleep 时间到。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 解锁
            lock.unlock();
        }
    }
}
